﻿GY.Bill.Message = {
    offerId:"",
    SendMessage: function () {
        var $this = $("#btnSend");
        var content = $("#txtContent").val().trim();

        if (content == "") {
            return;
        }

        $.ajax({
            type: "Post",
            url: $this.attr("data-url"),
            data: { BillId: $("#divMain").attr("data-billid"), OfferId: $this.attr("data-offerid"), Receiver: $this.attr("data-friendid"), Content: content },
            dataType: "html",
            async: false,
            success: function (result) {
                if (result.trim() != "") {
                    GY.Bill.Common.ShowSuccessTip();

                    $(".chat").hide();
                    //显示信息
                    $("#div_record_" + $this.attr("data-offerid")).append(result);

                    $("#txtContent").val("");
                }
            }
        });
    },
    GetMessage: function (offerId, $record) {
        $.ajax({
            type: "Post",
            url: $record.attr("data-url"),
            data: { offerId: offerId, billId: $("#divMain").attr("data-billid"), lastTime: $record.attr("data-lasttime") },
            dataType: "html",
            async: false,
            success: function (result) {
                if (result.trim() != "") {
                    //显示信息
                    $record.show();
                    $record.html(result);

                    var lastTime = $record.first(".one").attr("data-lasttime");
                    $record.attr("data-lasttime", lastTime);

                    $("#btn_message_show").show();
                }
            }
        });
    },
    GetMessageBefore: function() {
        //报价页面获取消息
        if (pageType == "o") {
            var offerId = $("#divMain").attr("data-offerid");

            if (offerId != "") {
                var $record = $("#div_record");
                GY.Bill.Message.GetMessage(offerId, $record);
            }
        }

        //询价页面获取聊天消息
        if (pageType == "c") {
            $("#div_offer .one .record").each(function () {
                var recordOfferId = $(this).attr("data-offerid");

                if (recordOfferId != "") {
                    GY.Bill.Message.GetMessage(recordOfferId, $(this));
                }
            });
        }
    }
};
(function () {
    $(function () {
        //在线交谈
        $("#div_offer .talk .sendmessage").on("tap", function (event) {
            $(".chat").show();

            $("#btnSend").attr("data-friendid", $(this).attr("data-friendid"));
            $("#btnSend").attr("data-offerid", $(this).attr("data-offerid"));

            GY.Bill.Message.offerId = $(this).attr("data-offerid");
            //messageHub.server.pushMessage($("#divMain").attr("data-billid"), pageType);

            event.stopPropagation();
        });
        $(".chat").on("tap", function (event) {
            event.stopPropagation();
        });
        $(".chat").on("tap", "a", function () {
            $(".chat").hide();
        });
        $(document).on("tap", function () {
            $(".chat").hide();
        });

        //发送消息
        $("#btnSend").on("tap", function () {
            GY.Bill.Message.SendMessage();
        });

        /* var messageHub = $.connection.messageHub;
 
         //由服务器端通知调用
         messageHub.client.showMessage = function (result) {
             //调用刷新方法
             console.log(result);
             //messageHub.server.getOffer($("#divMain").attr("data-billid"));
         };
 
         $.connection.hub.start().done(function () {
             //通知服务端页面被打开
             messageHub.server.open($("#divMain").attr("data-billid"), pageType);
         });*/

        setInterval(GY.Bill.Message.GetMessageBefore, 5000);
    });
})();

